package com.roadjava.GDOUGym.controller;
import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.dto.RecordDTO;
import com.roadjava.GDOUGym.bean.entity.RecordDO;
import com.roadjava.GDOUGym.bean.req.LoginReq;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.bean.res.TableResult;
import com.roadjava.GDOUGym.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@Slf4j
@RequestMapping("/login")
public class LoginHandler {
    @Resource
    private RecordService recordService;

    /**
     * 跳转到登录页面
     */
    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 跳转到找回密码
     */
    @GetMapping("/toFindPass")
    public String toFindPass() {
        return "findPass";
    }


    /**
     * 跳转到主页面
     */
    @GetMapping("/toMain11")
    public String toMain11() {
        return "admin/main/main11";
    }
    @GetMapping("/toMain21")
    public String toMain21() {
        return "admin/main/main21";
    }
    @GetMapping("/toMain22")
    public String toMain22() {
        return "admin/main/main22";
    }
    @GetMapping("/toMain23")
    public String toMain23() {
        return "admin/main/main23";
    }
    @GetMapping("/toMain24")
    public String toMain24() {
        return "admin/main/main24";
    }
    @GetMapping("/toMain25")
    public String toMain25() {
        return "admin/main/main25";
    }
    @GetMapping("/toMain31")
    public String toMain31() {
        return "admin/main/main31";
    }
    @GetMapping("/toMain41")
    public String toMain41() {
        return "admin/main/main41";
    }
    /**
     * 跳转到登录页面
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }

    /**
     * 执行登录
     * @param loginReq
     * @param session
     * @return
     */
    public List<RecordDO> recordDOList = new ArrayList<>();
    @PostMapping("/login") @ResponseBody
    public ResultDTO<ResultDTO<RecordDO>> login(@RequestBody LoginReq loginReq,
                                                              HttpSession session) {
        String ID = loginReq.getLoginID();
        String pass = loginReq.getLoginPass();
        // 管理员登录
        if (!StringUtils.hasText(ID) || !StringUtils.hasText(pass)) {
            return ResultDTO.buildFailure("用户名和密码不能为空");
        }
        // 执行校验
        RecordDO recordDO = new RecordDO();
        recordDO.setUserNo(ID);
        recordDO.setUserPassword(pass);
        ResultDTO<RecordDO> resultDTO = recordService.validateLogin(recordDO);

        if (resultDTO.getSuccess()) {
            // 校验成功
            RecordDO dos = resultDTO.getData();
            //判断此用户是否登录
            for(int i =0;i<recordDOList.size();i++){
                if(recordDOList.get(i).getUserNo() == dos.getUserNo()){
                    return ResultDTO.buildFailure("此用户已登录");
                }else{
                    recordDOList.add(dos);
                }
            }
            session.setAttribute("loginList",recordDOList);

            return ResultDTO.buildSuccess(resultDTO);
        }
        return ResultDTO.buildFailure(resultDTO.getErrMsg());
    }

    @PostMapping("/loginPhone") @ResponseBody
    public ResultDTO<ResultDTO<RecordDO>> loginPhone(@RequestBody LoginReq loginReq,
                                                              HttpSession session) {
        String phone = loginReq.getPhone();
        // 执行校验
        RecordDO recordDO = new RecordDO();
        recordDO.setTelephone(phone);
        ResultDTO<RecordDO> resultDTO = recordService.loginPhone(recordDO);
        System.out.println("resultDTO = " + resultDTO);

        if (resultDTO.getSuccess()) {
            // 校验成功
            RecordDO dos = resultDTO.getData();
            //判断此用户是否登录
            for(int i =0;i<recordDOList.size();i++){
                if(recordDOList.get(i).getUserNo() == dos.getUserNo()){
                    return ResultDTO.buildFailure("此用户已登录");
                }else{
                    recordDOList.add(dos);
                }
            }
            session.setAttribute("loginList",recordDOList);

            return ResultDTO.buildSuccess(resultDTO);
        }
        return ResultDTO.buildFailure(resultDTO.getErrMsg());
    }


}
